namespace WebApi.Modules.Common;

using Microsoft.Extensions.DependencyInjection;

/// <summary>
///     Versioning Extensions.
/// </summary>
public static class VersioningExtensions
{
    /// <summary>
    ///     Method that adds versioning to the api.
    /// </summary>
    public static IServiceCollection AddVersioning(this IServiceCollection services)
    {
        services.AddApiVersioning(
            options =>
            {
                    // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
                    options.ReportApiVersions = true;
            });
        services.AddVersionedApiExplorer(
            options =>
            {
                    // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
                    // note: the specified format code will format the version as "'v'major[.minor][-status]"
                    options.GroupNameFormat = "'v'VVV";

                    // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
                    // can also be used to control the format of the API version in route templates
                    options.SubstituteApiVersionInUrl = true;
            });

        return services;
    }
}
